import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by abely on 16-2-18.
 */
public class DownloadPicDemo {
    public static void main(String[] args) throws IOException {
        String url = "http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fr=&sf=1&fmq=1450429175806_R&pv=&ic=0&nc=1&z=&se=&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%A3%81%E7%BA%B8#z=0&pn=&ic=0&st=-1&face=0&s=0&lm=-1";
        Document doc = null;
        Connection.Response resultImageResponse = null;
        doc = Jsoup.connect(url).get();
        //System.out.println(doc.toString());
        Elements links = doc.getElementsByTag("img");
//        Elements pngs = doc.select("");
//        System.out.println(pngs.size());
        System.out.println(links.size());
        int i=1;
        for (Element pic : links) {
            String picAddress = pic.attr("abs:src");
            resultImageResponse = Jsoup.connect(picAddress).ignoreContentType(true).execute();
            FileOutputStream out = null;
            FileUtil.CreateFile("/home/abely/Pic",i+"."+StringUtil.getSuffix(picAddress));
            out = new FileOutputStream(new java.io.File("/home/abely/Pic/"+i+"."+StringUtil.getSuffix(picAddress)));
            out.write(resultImageResponse.bodyAsBytes());
            out.close();
            i++;
        }
        System.out.println(i);
    }
}
